<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/21
 * Time: 上午9:21
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class PageController extends AdminBaseController{
	/**
	 * 单页列表
	 */
	public function index(){
		$this -> display();
	}
	public function pageList(){
		$page = I('post.page/d',1);

		$MPage = M('Page');
		// 查询满足要求的总记录数
		$count = $MPage -> count();

		$limit = 8;
		$pages = ceil($count / $limit);
		$start = empty($page - 1) ? 0 : (($page - 1) * $limit);

		$list = $MPage  ->  order('sort,id') -> limit($start.','.$limit) -> select();

		//  传给laypage分页js 的列表内容
		$content = '';
		foreach($list as $value){
			$content .= '<tr><td>' . $value['id'] . '</td><td>' . $value['title'] . '</td><td>' . $value['name'] . '</td><td>' . $value['link'] . '</td><td>' . $value['keyword'] . '</td><td>' . $value['description'] . '</td><td>"' . mb_substr($value['content'],0,18,'utf-8') . '"...</td><td>' . $value['sort'] . '</td><td><div class="col-md-12"><a class="btn btn-sm btn-outline btn-warning" href="' . U('Admin/Page/edit/id/' .$value['id']) . '"><i class="fa fa-edit"></i> 修改</a><a class="btn btn-sm btn-outline btn-danger" href="javascript:del(' . $value['id'] . ')"><i class="fa fa-trash-o"></i> 删除</a></div></td></tr>';
		}
		$data = [
				'pages' => $pages,
				'content' => $content,
		];
		echo json_encode($data);
	}

	/**
	 * 单页添加表单
	 */
	public function add(){
		$this -> display();
	}
	/**
	 * 单页修改表单
	 */
	public function edit(){
		$id = I('get.id/d',0);
		$MPageInfo = M('Page') -> where(['id' => $id]) -> find();

		$this -> assign('pageInfo',$MPageInfo);
		$this -> display('Page_add');
	}

	/**
	 * 添加修改保存
	 */
	public function Save(){
		if(IS_POST && IS_AJAX){
			$id = I('post.id/d',0);

			$title = I('post.title/s','');
			$name = I('post.name/s','');
			$content = I('post.content/s','');
			$keyword = I('post.keyword/s','');
			$description = I('post.description/s','');
			$link = I('post.link/s','');
			$sort = I('post.sort/d', 1);

			if(!$title){
				$this -> ajaxReturn(['status' => 1, 'msg' => '单页标题不能为空']);
			}
			if(!$name){
				$this -> ajaxReturn(['status' => 1, 'msg' => '单页英文名称不能为空']);
			}
			if(!$content){
				$this -> ajaxReturn(['status' => 1, 'msg' => '单页内容不能为空']);
			}
			if(!$keyword){
				$this -> ajaxReturn(['status' => 1, 'msg' => '单页关键词不能为空']);
			}
			if(!$description){
				$this -> ajaxReturn(['status' => 1, 'msg' => '单页描述不能为空']);
			}
			if(!$link){
				$this -> ajaxReturn(['status' => 1, 'msg' => '单页链接不能为空']);
			}
			if(!$sort){
				$this -> ajaxReturn(['status' => 1, 'msg' => '单页排序不能为空']);
			}

			$data = [
					'title' => $title,
					'name' => $name,
					'link' => $link,
					'content' => $content,
					'keyword' => $keyword,
					'description' => $description,
					'sort' => $sort
			];

			if(!$id){
				/**
				 * ID为空 添加
				 */
				$MPage = M('Page');
				$isOk = $MPage -> add($data);
			}else{
				/**
				 * ID不为空  修改
				 */
				$MPage = M('Page');
				$isOk = $MPage -> where(['id' => $id]) -> setField($data);
			}

			if(!$isOk){
				$this -> ajaxReturn(['status' => 1, 'msg' => '操作失败']);
			}
			$this -> ajaxReturn(['status' => 0, 'msg' => '操作成功']);
		}else{
			$this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
		}
	}



	/**
	 * 删除单页
	 */
	public function del(){
		if(IS_AJAX && IS_POST){
			$id = I('post.id/d');
			if(empty($id)){
				$this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
			}
			$MPage = M('Page');

			$isDel = $MPage -> delete($id);

			if(!$isDel){
				$this -> ajaxReturn(['status' => 1, 'msg' => '删除失败']);
			}
			$this -> ajaxReturn(['status' => 0, 'msg' => '删除成功']);
		}else{
			if(empty($id)){
				$this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
			}
		}
	}
}